package com.ruoyi.web.controller.aggregation;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.aggregation.dto.PageDTO;
import com.ruoyi.system.domain.aggregation.Order;
import com.ruoyi.system.service.aggregation.IOrderService;

/**
 * 订单Controller
 * 
 * @author ruoyi
 * @date 2025-01-01
 */
@RestController
@RequestMapping("/agg/order")
public class OrderController
{
    @Autowired
    private IOrderService orderService;

    /**
     * 分页查询订单
     */
    @PostMapping("/page")
    public AjaxResult page(@RequestBody PageDTO pageDTO)
    {
        try
        {
            PageHelper.startPage(pageDTO.getPageIndex().intValue(), pageDTO.getPageSize().intValue());
            Order query = new Order();
            List<Order> list = orderService.selectOrderList(query);
            PageInfo<Order> pageInfo = new PageInfo<>(list);
            return AjaxResult.success(pageInfo);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查询订单列表（导出用）
     */
    @PostMapping("/list")
    public AjaxResult list(@RequestBody Order order)
    {
        try
        {
            List<Order> list = orderService.selectOrderList(order);
            return AjaxResult.success(list);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 创建订单
     */
    @PostMapping("/create")
    public AjaxResult create(@RequestBody Order order)
    {
        try
        {
            Order newOrder = orderService.createOrder(order);
            return AjaxResult.success(newOrder);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 支付订单
     */
    @PostMapping("/pay")
    public AjaxResult pay(@RequestParam Long orderId, 
                         @RequestParam Long payAmount,
                         @RequestParam Long payPointAmount)
    {
        try
        {
            orderService.payOrder(orderId, payAmount, payPointAmount);
            return AjaxResult.success("支付成功");
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查询订单详情
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo(@RequestParam Long id)
    {
        try
        {
            Order order = orderService.selectOrderById(id);
            return AjaxResult.success(order);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
}

